package jplat.tools.asyntask;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import jplat.tools.config.JAppConfig;
import jplat.tools.string.XLog;

/**
 * 不获取返回结果的异步任务.no-result.
 * @author zhangcq
 * @date Jan 10, 2017
 * @comment
 */
public class JAsynTaskExecutor
{
//	private static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
	private static ArrayBlockingQueue<Runnable> taskQueue = new  ArrayBlockingQueue<Runnable>(JAppConfig.ASYN_POOL_MAX_QUEUE);
	private static ThreadPoolExecutor executor = new ThreadPoolExecutor( JAppConfig.ASYN_POOL_CORE_SIZE, JAppConfig.ASYN_POOL_MAX_SIZE, 1500, TimeUnit.MILLISECONDS, taskQueue );
	static
	{
		executor.allowCoreThreadTimeOut(true);
	}
	
	//throws RejectedExecutionException
	public static void execute( Runnable task )
	{
		executor.submit(task);
	}
	
	public static void main(String args[])
	{
		XLog.log("size:"+executor.getPoolSize());
		XLog.log("max:"+executor.getMaximumPoolSize());
		System.out.println("stop:"+executor.allowsCoreThreadTimeOut());
		for ( int i = 0; i < 10000; ++i )
		{
			JAsynTaskExecutor.execute(new JTestTask());
			System.out.println("add:"+i);
		}
		
		System.out.println("task-size:"+taskQueue.size());
	}
}
